package com.quanshu.shop.order.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.support.entity.MyOrderEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/9/11
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class OrderListAdapter extends BaseQuickAdapter<MyOrderEntity, BaseViewHolder> {
    private Context mContext;

    public OrderListAdapter(@Nullable List<MyOrderEntity> data, Context mContext1) {
        super(R.layout.item_order_list, data);
        mContext = mContext1;
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderEntity item) {
        helper.setText(R.id.txt1, "订单号：" + item.getOrder_no());//订单编号
        Glide.with(mContext).load(Config.URL_HOST_SHOP + item.getImg()).into((ImageView) helper.getView(R.id.img1));//商品图片
        helper.setText(R.id.txt4, item.getGoods_name());//商品名称
        try {
            if (!TextUtils.isEmpty(item.getGoods_array())) {
                JSONObject jsonObject = new JSONObject(item.getGoods_array());
                helper.setText(R.id.txt6, jsonObject.getString("goodsno"));//商品编号
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.txt8, "X" + item.getGoods_nums());
        helper.setText(R.id.txt2, item.getOrderStatuText());
        TextView textViewPay1 = helper.getView(R.id.money1);//牛积分
        TextView textViewPay2 = helper.getView(R.id.money2);//现金
        TextView textViewPay3 = helper.getView(R.id.money3);//金豆
        TextView textViewPay11 = helper.getView(R.id.money4);//牛积分
        TextView textViewPay21 = helper.getView(R.id.money5);//现金
        TextView textViewPay31 = helper.getView(R.id.money6);//金豆

        textViewPay1.setText(item.getGoods_price());
        textViewPay2.setText(item.getGoods_price());
        textViewPay3.setText(item.getGoods_price());
        textViewPay11.setText(item.getSum_price());
        textViewPay21.setText(item.getSum_price());
        textViewPay31.setText(item.getSum_price());
        switch (item.getPayment_options()) {
            case 1://牛积分
                setPayState(textViewPay1, textViewPay2, textViewPay3);
                setPayState(textViewPay11, textViewPay21, textViewPay31);
                break;
            case 2://现金
                setPayState(textViewPay2, textViewPay1, textViewPay3);
                setPayState(textViewPay21, textViewPay11, textViewPay31);
                break;
            case 3://金豆
                setPayState(textViewPay3, textViewPay1, textViewPay2);
                setPayState(textViewPay31, textViewPay11, textViewPay21);
                break;
        }
        LinearLayout linearLayout1 = helper.getView(R.id.cl1);//待付款显示
        LinearLayout linearLayout2 = helper.getView(R.id.cl2);//待发货显示
        LinearLayout linearLayout3 = helper.getView(R.id.cl3);//已发货显示
        LinearLayout linearLayout5 = helper.getView(R.id.cl5);//已取消显示
        LinearLayout linearLayout6 = helper.getView(R.id.cl6);//退款中显示
        switch (item.getOrderStatu()) {
            case 2://待付款
                setOrderState(linearLayout1, linearLayout2, linearLayout3, linearLayout5, linearLayout6);
                break;
            case 4://待发货
                setOrderState(linearLayout2, linearLayout1, linearLayout3, linearLayout5, linearLayout6);
                break;
            case 3://待收货
                setOrderState(linearLayout3, linearLayout1, linearLayout2, linearLayout5, linearLayout6);
                break;
            case 6://已完成
                setOrderState(linearLayout5, linearLayout1, linearLayout2, linearLayout3, linearLayout6);
                break;
            case 5://已取消
                setOrderState(linearLayout5, linearLayout1, linearLayout2, linearLayout3, linearLayout6);
                break;
            case 14://退款中
                setOrderState(linearLayout6, linearLayout2, linearLayout3, linearLayout5, linearLayout1);
                break;
        }
        if (Integer.parseInt(item.getIs_service()) == 0) {
            //不包含大礼包
            helper.setVisible(R.id.txt_tips, false);
        } else {
            //包含大礼包
            helper.setVisible(R.id.txt_tips, true);
        }
        helper.addOnClickListener(R.id.txt_cancel_order);
        helper.addOnClickListener(R.id.txt_pay_order);
        helper.addOnClickListener(R.id.txt_apply_order);
        helper.addOnClickListener(R.id.txt_ok_order);
        helper.addOnClickListener(R.id.txt_delete_order);
        helper.addOnClickListener(R.id.txt_refund_order);
        helper.addOnClickListener(R.id.root_cl);
    }

    private void setPayState(TextView txt1, TextView textView2, TextView textView3) {
        txt1.setVisibility(View.VISIBLE);
        textView2.setVisibility(View.INVISIBLE);
        textView3.setVisibility(View.INVISIBLE);
    }

    private void setOrderState(LinearLayout linearLayout1, LinearLayout linearLayout2, LinearLayout linearLayout3, LinearLayout linearLayout4, LinearLayout linearLayout5) {
        linearLayout1.setVisibility(View.VISIBLE);
        linearLayout2.setVisibility(View.GONE);
        linearLayout3.setVisibility(View.GONE);
        linearLayout4.setVisibility(View.GONE);
        linearLayout5.setVisibility(View.GONE);
    }
}
